﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace crm_library.Extenstions
{
	public static class TableExtensions
	{
		public static void AddGeneratedRow(this Table table, params Object[] objects)
		{
			var controls = new List<Control>();

			foreach (var obj in objects)
			{
				if (obj is Control) controls.Add(obj as Control);
				else if (obj is string) controls.Add((obj as string).ToLiteral());
				else controls.Add((obj.ToString()).ToLiteral());
			}

			table.AddGeneratedRow(controls.ToArray());
		}

		public static void AddGeneratedRow(this Table table, params Control[] controls)
		{
			var row = new TableRow();
			foreach (var control in controls)
			{
				row.AddGeneratedTableCell(control);
			}
			table.Rows.Add(row);
		}

		public static void AddGeneratedTableCell(this TableRow row, Control ctrl)
		{
			var cell = new TableCell();
			cell.Controls.Add(ctrl);
			row.Cells.Add(cell);
		}

		public static void AddGeneratedHeaderRow(this Table table, params Object[] objects)
		{
			var controls = new List<Control>();

			foreach (var obj in objects)
			{
				if (obj is Control) controls.Add(obj as Control);
				else if (obj is string) controls.Add((obj as string).ToLiteral());
			}

			table.AddGeneratedHeaderRow(controls.ToArray());
		}

		public static void AddGeneratedHeaderRow(this Table table, params Control[] controls)
		{
			var row = new TableRow();
			foreach (var control in controls)
			{
				row.AddGeneratedTableHeaderCell(control);
			}
			table.Rows.Add(row);
		}

		public static void AddGeneratedTableHeaderCell(this TableRow row, Control ctrl)
		{
			var cell = new TableHeaderCell();
			cell.Controls.Add(ctrl);
			row.Cells.Add(cell);
		}

	}
}
